package Sort.MergeSort;

/** 归并排序的一个简单例子
 * @program: jvm
 * @description
 * @author: tkk fendoukaoziji
 * @create: 2019-05-15 18:40
 **/
public class TestMerge {
    public static void main(String[] args) {
        int[] a={23,47,81,95};
        int[] b={7,14,39,55,62,74};
        int[] c=new int[10];
        merge(a,b,c);
        display(c);
    }

    private static void merge(int[] a, int[] b, int[] c) {
        int aIndex=0,bIndex=0,cIndex=0;
        while(aIndex<a.length&&bIndex<b.length){  //沿着数组a和b走，比较它们的数据项，并且复制他们比较小的数据项到数组c中
            if(a[aIndex]<b[bIndex]){
                c[cIndex++]=a[aIndex++];
            }else{
                c[cIndex++]=b[bIndex++];
            }
        }
        while(aIndex<a.length){ //b 为空
            c[cIndex++]=a[aIndex++];
        }
        while(bIndex<b.length){// a为空
            c[cIndex++]=b[bIndex++];
        }

    }
    public static void display(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println("");
    }
}
